//
//  EruptController.m
//  FastDoctor
//
//  Created by apple on 16/7/8.
//  Copyright © 2016年 sc. All rights reserved.
//

#import "EruptController.h"
#import "EruptDTO.h"
#import "EruptDiseaseCell.h"
#define kFilePath(fileName) ([NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:fileName])
@interface EruptController ()<EruptDiseaseCellDelegate>

@property (nonatomic,strong) NSArray <EruptDTO *> *eruptList;

@property (nonatomic,strong) NSMutableArray *choseList;
@property(nonatomic,assign)NSUInteger idx;
@end

@implementation EruptController
-(NSMutableArray *)choseList{
    if (!_choseList) {
        _choseList = [NSKeyedUnarchiver unarchiveObjectWithFile:kFilePath(@"chose.data")];
        if (!_choseList) {
            _choseList = [NSMutableArray array];
        }
    }
    return _choseList;
}
- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self setupBaseSetting];
    
    [EruptDTO getEruptListSuccess:^(NSArray<EruptDTO *> *eruptList) {
        
        self.eruptList = eruptList;
        [SVProgressHUD dismiss];
    } error:^{
        
        [SVProgressHUD showErrorWithStatus:@"网络错误"];
    }];
   
}

- (void)setupBaseSetting {
    
    self.tableView.tableFooterView = [[UIView alloc]init];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"确定" style:UIBarButtonItemStyleDone target:self action:@selector(didClickSureItem)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor blackColor];
    
}

- (void)setEruptList:(NSArray<EruptDTO *> *)eruptList {
    
    _eruptList = eruptList;
    
    [self.tableView reloadData];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    [SVProgressHUD dismiss];
}

#pragma mark - tableViewController delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.eruptList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *identifier = [NSString stringWithFormat:@"cell%ld",(long)indexPath.row];
    
    EruptDiseaseCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (!cell) {
        
        cell = [[EruptDiseaseCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    
    
    EruptDTO *eruptModel = self.eruptList[indexPath.row];
    
    cell.eruptModel = eruptModel;
    cell.eruptDelegate = self;
    [cell.contentView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj isKindOfClass:[UILabel class]]) {
            
            UILabel *label =obj;
            NSArray *array = [NSMutableArray array];
            array = self.choseList.copy;
            [array enumerateObjectsUsingBlock:^(NSString*  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if ([label.text isEqualToString: obj]) {
                    [cell.contentView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                        if ([obj isKindOfClass:[UIButton class]]) {
                            UIButton *button = obj;
                            button.selected = 1;
                            [button setTitle:@"已订阅" forState:UIControlStateNormal];
                            [button setTitleColor:RED_COLOR forState:UIControlStateNormal];
                        }
                    }];
                    
                    
                    
                }}];
            
        }}];
    
    
return cell;
}

- (void)didClickEruptDiseaseCellButton:(UIButton *)button withLabel:(UILabel *)label {
    
    
    
  if ([button.titleLabel.text isEqualToString:@"订阅"]){
        [self.choseList addObject:label.text];
        [button setTitle:@"已订阅" forState:UIControlStateNormal];
        [button setTitleColor:RED_COLOR forState:UIControlStateNormal];
    }else {
        [self.choseList removeObject:label.text];
        [button setTitle:@"订阅" forState:UIControlStateNormal];
        [button setTitleColor:WHITE_COLOR forState:UIControlStateNormal];
    }
  
    
    [self.tableView reloadData];
    

}

//截取字符串返回给上一个控制器
- (void)didClickSureItem
{
    
    [NSKeyedArchiver archiveRootObject:self.choseList toFile:kFilePath(@"chose.data")];
    [[NSUserDefaults standardUserDefaults]synchronize];
    NSMutableString *mStr = [NSMutableString string];
    
    for (NSString *str in self.choseList)
    {
        //拼接字符串用于区分多个并发症！！！！勿改！！
        [mStr appendString:[NSString stringWithFormat:@"%@、",str]];
    }

    if (self.block)
    {
        if (mStr.length > 0)
        {
            self.block([mStr substringToIndex:mStr.length -1]);
        }
        else
        {
            self.block(@"");
        }
    }
    [self.navigationController popViewControllerAnimated:YES];
}



@end
